﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ProjectQuaDep.Services
{
    public partial class CropImages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string kq = "";
            int Width = int.Parse(Request.QueryString["width"].ToString());
            int Height = int.Parse(Request.QueryString["height"].ToString()); ;
            string sourceFilePath = Server.MapPath(Request.QueryString["src"].ToString());
            string fileName = Path.GetFileName(sourceFilePath);
            string linkShowKq = "/Upload/Web/thumbimages/" + fileName + "_" + Width + "_" + Height + ".jpg";
            string saveFilePath = Server.MapPath(linkShowKq);
            if (File.Exists(saveFilePath) == true)
            {
                kq = saveFilePath;
            }
            else
            {
                CropImage(Width, Height, sourceFilePath, saveFilePath);
                kq = saveFilePath;
            }
            Response.ContentType = "image/jpeg";
            Response.WriteFile(kq);
        }
        public static void CropImage(int Width, int Height, string sourceFilePath, string saveFilePath)
        {
            // variable for percentage resize 
            float percentageResize = 0;
            float percentageResizeW = 0;
            float percentageResizeH = 0;

            // variables for the dimension of source and cropped image 
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            // Create a bitmap object file from source file 
            Bitmap sourceImage = new Bitmap(sourceFilePath);

            // Set the source dimension to the variables 
            int sourceWidth = sourceImage.Width;
            int sourceHeight = sourceImage.Height;

            // Calculate the percentage resize 
            percentageResizeW = ((float)Width / (float)sourceWidth);
            percentageResizeH = ((float)Height / (float)sourceHeight);

            // Checking the resize percentage 
            if (percentageResizeH < percentageResizeW)
            {
                percentageResize = percentageResizeW;
                destY = System.Convert.ToInt16((Height - (sourceHeight * percentageResize)) / 2);
            }
            else
            {
                percentageResize = percentageResizeH;
                destX = System.Convert.ToInt16((Width - (sourceWidth * percentageResize)) / 2);
            }

            // Set the new cropped percentage image
            int destWidth = (int)Math.Round(sourceWidth * percentageResize);
            int destHeight = (int)Math.Round(sourceHeight * percentageResize);

            // Create the image object 
            using (Bitmap objBitmap = new Bitmap(Width, Height))
            {
                objBitmap.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
                using (Graphics objGraphics = Graphics.FromImage(objBitmap))
                {
                    // Set the graphic format for better result cropping 
                    objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    objGraphics.DrawImage(sourceImage, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

                    // Save the file path, note we use png format to support png file 
                    objBitmap.Save(saveFilePath, ImageFormat.Jpeg);
                }
            }
        }
    }
}